home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-03-19 | 3.8 KB | 131 lines | [TEXT/R*ch] |
- /*
- ** Copyright: © Michael S. Engber, 1994. All Rights Reserved.
- **
- ** SearchUtils.f
- **
- ** Binary search routines
- **
- */
-
- //DataUtils.f must be loaded first
- // (defines some constants)
-
- //
- // utils for searching array of frames rep
- //
-
- func BSearchArrayOfFrames(array,key)
- begin
- local lPos := 0;
- local rPos := Length(array) - 1;
- local mPos;
-
- while lPos <= rPos do
- begin
- mPos := (lPos + rPos) DIV 2;
- mVal := array[mPos].slot2;
- if mVal > key then
- rPos := mPos - 1;
- else
- lPos := mPos + 1;
- end;
-
- if rPos>=0 AND StrEqual(array[rPos].slot2,key) then rPos;
- end;
-
- /*
- // test code
- testData := [{slot2: "b"},{slot2: "d"},{slot2: "f"}];
- if BSearchArrayOfFrames(testData,"a") <> nil then Print("*error*");
- if BSearchArrayOfFrames(testData,"b") <> 0 then Print("*error*");
- if BSearchArrayOfFrames(testData,"c") <> nil then Print("*error*");
- if BSearchArrayOfFrames(testData,"d") <> 1 then Print("*error*");
- if BSearchArrayOfFrames(testData,"e") <> nil then Print("*error*");
- if BSearchArrayOfFrames(testData,"f") <> 2 then Print("*error*");
- if BSearchArrayOfFrames(testData,"g") <> nil then Print("*error*");
- */
-
-
- //
- // utils for searching array of arrays rep
- //
-
- func BSearchArrayOfArrays(array,key)
- begin
- local lPos := 0;
- local rPos := Length(array) - 1;
- local mPos;
-
- while lPos <= rPos do
- begin
- mPos := (lPos + rPos) DIV 2;
- mVal := array[mPos][1];
- if mVal > key then
- rPos := mPos - 1;
- else
- lPos := mPos + 1;
- end;
-
- if rPos>=0 AND StrEqual(array[rPos][1],key) then rPos;
- end;
-
- /*
- // test code
- testData := [[nil,"b"],[nil,"d"],[nil,"f"]];
- if BSearchArrayOfArrays(testData,"a") <> nil then Print("*error*");
- if BSearchArrayOfArrays(testData,"b") <> 0 then Print("*error*");
- if BSearchArrayOfArrays(testData,"c") <> nil then Print("*error*");
- if BSearchArrayOfArrays(testData,"d") <> 1 then Print("*error*");
- if BSearchArrayOfArrays(testData,"e") <> nil then Print("*error*");
- if BSearchArrayOfArrays(testData,"f") <> 2 then Print("*error*");
- if BSearchArrayOfArrays(testData,"g") <> nil then Print("*error*");
- */
-
-
- //
- // utils for searching binary object rep
- //
-
- //from DataUtils.f - which must be loaded first
- //constant kSlot1Offset := 0; //a long (4 bytes)
- //constant kSlot2Offset := 4; //a cstring of (8 bytes = up to 7 chars + terminating null)
- //constant kSlot3Offset := 12; //a word (2 bytes)
- //constant kSlot4Offset := 14; //a byte
- //constant kDatumSize := 15;
-
- func BSearchBinaryObj(binObj,key)
- begin
- local lPos := 0;
- local rPos := (Length(binObj) DIV kDatumSize) - 1;
- local mPos;
-
- while lPos <= rPos do
- begin
- mPos := (lPos + rPos) DIV 2;
- mVal := ExtractCString(binObj,mPos*kDatumSize + kSlot2Offset);
- if mVal > key then
- rPos := mPos - 1;
- else
- lPos := mPos + 1;
- end;
-
- if rPos>=0 AND StrEqual(ExtractCString(binObj,rPos*kDatumSize + kSlot2Offset),key) then rPos*kDatumSize;
- end;
-
- /*
- // test code
- testData := SetLength("\u000000006200000000000000000000000000006400000000000000000000000000006600000000000000000000",3*kDatumSize);
- if BSearchBinaryObj(testData,"a") <> nil then Print("*error*");
- if BSearchBinaryObj(testData,"b") <> 0*kDatumSize then Print("*error*");
- if BSearchBinaryObj(testData,"c") <> nil then Print("*error*");
- if BSearchBinaryObj(testData,"d") <> 1*kDatumSize then Print("*error*");
- if BSearchBinaryObj(testData,"e") <> nil then Print("*error*");
- if BSearchBinaryObj(testData,"f") <> 2*kDatumSize then Print("*error*");
- if BSearchBinaryObj(testData,"g") <> nil then Print("*error*");
- */
-
- //define constants - we really want to use these fns at run time
- DefConst('kBSearchArrayOfFramesFn, functions.BSearchArrayOfFrames);
- DefConst('kBSearchArrayOfArraysFn, functions.BSearchArrayOfArrays);
- DefConst('kBSearchBinaryObjFn, functions.BSearchBinaryObj);
-